
package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoBusqueda2;



public class Table2 extends javax.swing.JFrame {
      private ClienteLnLocal clienteControl;
    private List<Cliente> lista;
    private DefaultTableModel modelo;
    private JFrame ventana2;
    

    
    public Table2(ClienteLnLocal clienteControl) {
           this.clienteControl= clienteControl;
        initComponents();
          setLocationRelativeTo(null);
       setResizable(true);
       setTitle("Menu Principal");
     
      ((JPanel)getContentPane()).setOpaque(false); 
      ImageIcon uno=new ImageIcon(this.getClass().getResource("/imagenes/fondo.jpg"));
      JLabel fondo= new JLabel(); 
      fondo.setIcon(uno); getLayeredPane().add(fondo,JLayeredPane.FRAME_CONTENT_LAYER); 
      fondo.setBounds(0,0,uno.getIconWidth(),uno.getIconHeight());
      
      
      
       jButton1.setBackground(Color.BLACK);
  
         
        String nombres[]={"id","Nombre","telefono","direccion","sexo"};
        
        modelo = new DefaultTableModel(nombres, 0){

public boolean isCellEditable(int row, int column){
return false;
}
        };
        jTable2.setModel(modelo);
        lista=clienteControl.getTodos();
                
        
        for(Cliente c: lista){
            String renglon[]= new String[5];
            renglon[0]=c.getid()+"";
            renglon[1]=c.getnombre();
            renglon[2]=c.gettelefono()+"";
            renglon[3]=c.getdireccion();
            renglon[4]=c.getsexo();
            modelo.addRow(renglon);
        }
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jRadioButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jRadioButton1.setText("start");

        jRadioButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jRadioButton2.setText("contain");

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        jScrollPane1.setOpaque(false);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "nombre", "id", "telefono", "direccion", "sexo"
            }
        ));
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable2);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jButton1.setForeground(new java.awt.Color(240, 240, 240));
        jButton1.setText("Back");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jRadioButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jRadioButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(86, 86, 86)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addComponent(jRadioButton2)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
       List<Cliente> tmp= new ArrayList<>();
        if(jRadioButton1.isSelected()){
           tmp = clienteControl.getPorNombre(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda2.CONTIENE2);
        }else{
            tmp = clienteControl.getPorNombre(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda2.INICIAR2);
        }
         String nombres[]={"id","nombre","telefono","direccion","sexo"};
        DefaultTableModel modelo= new DefaultTableModel(nombres, 0){
        
        public boolean isCellEditable(int row, int column){
        return false;
        }
        };
        
        
        
        
        
        for(Cliente c: tmp){
           
        
            String renglon[]= new String[5];
            renglon[0]=c.getid()+"";
            renglon[1]=c.getnombre();
            renglon[2]=c.gettelefono()+"";
            renglon[3]=c.getdireccion();
            renglon[4]=c.getsexo();
            modelo.addRow(renglon);
        }
        
        jTable2.setModel(modelo);
        lista=tmp;
       
           
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
         if (evt.getClickCount()==2){
        Cliente c= lista.get(jTable2.getSelectedRow());
        if(ventana2==null){
        ventana2 = new Dialogo2(c, clienteControl);
        }else{
        ventana2.dispose();
        ventana2 = new Dialogo2(c, clienteControl);
        }
        ventana2.setVisible(true);
        }
        
        
                                      

    }//GEN-LAST:event_jTable2MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
